﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oxite.Models;
using Oxite.Infrastructure;
using ECube.SEC.Models;
using ECube.SEC.Repositories;
namespace ECube.SEC.Services
{
    public class AuthorizeService : IAuthorizeService
    {
        private static Dictionary<IUser, List<String>> cache;
        private IAuthorizeRepository repository;
        public AuthorizeService(IAuthorizeRepository repository)
        {
            this.repository = repository;
            if (cache == null)
            {
                cache = new Dictionary<IUser, List<String>>();
            }
        }       

        #region IAuthorizeService 成员

        public List<Permission> Permissions()
        {
            return repository.GetPermissions();            
        }

        public List<string> GetPermissionsForUser(IUser user)
        {
            User user1 = user as User;
            if (user1 != null)
            {
                List<String> list = cache.ContainsKey(user1) ? cache[user1] : null;
                if (list == null)
                {
                    list = repository.GetPermissionsForUser(user1);
                    cache.Add(user1, list);
                }
                return list;
            }
            return new List<string>(0);
        }

        #endregion
    }
}
